﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 네트워크 패킷을 보낼때 사용되는 구조체.
/// LSH/2013.11.21
/// </summary>
public struct NetMsg
{
	// private Member
    private string                          mMsgID;
    private Dictionary<string, string>      mDicField;
    private bool                            mRetry;
	private bool							mIsShowError;

	private object mCallBackAck;
    private object mCallBackErr;
	
    public void Reset()
    {
        mDicField  		= new Dictionary<string, string>();
        mMsgID          = "";
        mCallBackAck    = null;
        mCallBackErr = null;
		mIsShowError	= true;
    }

	public string MsgID {
        set { this.mMsgID = value; }
        get { return this.mMsgID; }
	}

	public System.Type Type { get; set; }
	
	public object CallbackAck {
        set { this.mCallBackAck = value; }
        get { return this.mCallBackAck; }
	}

    public object CallbackErr
    {
        set { this.mCallBackErr = value; }
        get { return this.mCallBackErr; }
    }

    public bool Retry
    {
        set { this.mRetry = value; }
        get { return this.mRetry;  }
    }

    public bool Crypto { get; set; }    //131203 hikim

	public bool IsShowError { get { return mIsShowError; } set { mIsShowError = value; } }
	
	public void AddField(string key, string value) {
        mDicField.Add(key, value);
	}

    public void AddField(string key, long value)
    {
        mDicField.Add(key, value.ToString());
    }

    public void AddField(string key, int value)
    {
        mDicField.Add(key, value.ToString());
    }

    public void AddField(string key, short value)
    {
        mDicField.Add(key, value.ToString());
    }

    public void AddField(string key, byte value)
    {
        mDicField.Add(key, value.ToString());
    }

    public void AddField(string key, bool value)
    {
        mDicField.Add(key, value.ToString());
    }

	public void AddField(string key, int[] value)
	{
		string commaCombineValue = "";
		bool bFlag = false;
		if(value!=null) {
			foreach (int nValue in value)
			{
				string strValue = nValue.ToString();
				if (bFlag)
				{
					commaCombineValue += ",";
				}

				commaCombineValue += strValue;
				bFlag = true;
			}
		}
		AddField(key, commaCombineValue);
	}

	public void AddField(string key, long[] value)
	{
		string commaCombineValue = "";
		bool bFlag = false;
		if(value!=null) {
			foreach (long nValue in value)
			{
				string strValue = nValue.ToString();
				if (bFlag)
				{
					commaCombineValue += ",";
				}

				commaCombineValue += strValue;
				bFlag = true;
			}
		}
		AddField(key, commaCombineValue);
	}
	
	public Dictionary<string, string> GetDicField() {
        return mDicField;
	}

    public string GetQueryString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (KeyValuePair<string, string> kvp in mDicField)
        {
            sb.Append(kvp.Key).Append('=').Append(kvp.Value).Append('&');
        }
        return sb.ToString();
    }

    public byte[] GetQueryStringBytes()
    {
        return Common.Encoding.GetBytes(GetQueryString());
    }
}
